package com.crack.test;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.SystemClock;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.crack.spansframe.AsyncLoadCallBack;
import com.crack.spansframe.ClickMovement;
import com.crack.spansframe.SpanSolve;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity {
    public static String TAG = "crack";
    public static String s1 = "[quote][b][del][color=red][size=150%]请仔细阅读规则在本版另发主题帖，不要直接回帖。" +
            "[/size][/color][/DEL][/b][/quote]<br/><br/>[collapse=[size=130%]点击[/size]][size=150%]四月是郊游的好季节，冬袍已封箱，夏衣尚未着。" +
            "<br/>[i]趁着这宜人的日子[/i]，远离城市的嘈杂，[del]奔向广阔的大自然吧！[/del][/size][/collapse]<br/><br/><br/>" +
            "[quote][align=center][img]http://img.ngacn.cc/attachments/mon_201604/18/428_571440c078615.jpg[/img][/align][/quote]" +
            "<br/><br/><br/>===========<br/>[quote][size=150%]本月内在本版分享与[b]踏青[/b]" +
            "相关的摄影照片或心情文字等[/size][color=red][size=150%]主题帖[/size][/color][size=120%]" +
            "可获得更多加分。[/size]<br/>[collapse=点击查看隐藏内容][list]<br/>[*][b][color=red][size=120%]" +
            "需发表的是主题帖并非在本帖直接回复。[/size][/color][/b]<br/>[*]内容需原创。<br/>" +
            "[*]发表完主题帖后可将链接回复至本帖。<br/>[*][b][color=red][size=120%]" +
            "主题帖标题需加上tag[四月踏青]。[/size][/color][/b]<br/>[*]本活动最终解释权归水区版务组。" +
            "<br/>[/list][/collapse][/quote]<br/>更多内容请点击[url=http://bbs.ngacn.cc]此处[/url]查看";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(Build.VERSION.SDK_INT>=21){
            ActionBar actionBar = getSupportActionBar();
            if (actionBar != null) {
                actionBar.setElevation(0);
            }
        }
        SpanSolve.injectContext(this);
        setContentView(R.layout.activity_main);
        final TextView textView = (TextView) findViewById(R.id.text);
        long start = SystemClock.currentThreadTimeMillis();
        SpanSolve ss = new SpanSolve(s1);
        final SpannableStringBuilder sp = ss.resolve();
        ss.callBack = new AsyncLoadCallBack() {
            @Override
            public void complete() {
                textView.setText(sp);
            }
        };
        SpanSolve.injectTextView(sp, textView);
        long end = SystemClock.currentThreadTimeMillis();
        long time = end - start;
        Log.i("crack", "solve-time===>>>" + time);
//        SpannableStringBuilder sssss = SpannableStringBuilder.valueOf("abc\ndef\nghj\n");
//        sssss.setSpan(new BulletSpan(5), 0, 3, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        sssss.setSpan(new BulletSpan(5), 4, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        sssss.setSpan(new BulletSpan(5), 8, sssss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        textView.setText(sssss);
        textView.setText(sp);
        textView.setMovementMethod(ClickMovement.getInstance());
/*        SpannableStringBuilder tt = SpannableStringBuilder.valueOf("abc\nabc");
        int column = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                (float) 200.0, getResources().getDisplayMetrics());

        SpannableStringBuilder textspan = new SpannableStringBuilder("Charges\nPrice\t$25.00\nShipping\t$155.08\nTax\t$5.11\n");
        textspan.setSpan(new TabStopSpan.Standard(column), 0, textspan.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        textView.setText(textspan);*/
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                long start = SystemClock.currentThreadTimeMillis();
                textView.setText(sp);
                long end = SystemClock.currentThreadTimeMillis();
                long time = end - start;
                Log.i("crack", "all-time===>>>" + time);
            }
        });
    }

    private void setSpan(String string) {

    }

    private void format(SpannableStringBuilder sp) {
        Drawable d = getResources().getDrawable(R.mipmap.ic_launcher);
        d.setBounds(0, 0, d.getIntrinsicHeight(), d.getIntrinsicWidth());
        Pattern p = Pattern.compile("\\[url\\](.*?)\\[/url\\]");
        Matcher m = p.matcher(sp);
        while (m.find()) {
            int start = m.start(0);
            int end = m.end(0);
            Log.i(TAG, start + "," + end + "----->" + m.group(0));
            sp.setSpan(new ImageSpan(d), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        p = Pattern.compile("\\[img\\](.*?)\\[/img\\]");
        m = p.matcher(sp);
        while (m.find()) {
            int start = m.start(0);
            int end = m.end(0);
            sp.setSpan(new ImageSpan(d), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
    }
}
